package com.saop.core.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.Nullable;

import com.saop.core.SAOP;
import com.saop.core.api.DialogHandler;
import com.saop.core.api.DialogListener;
import com.saop.core.api.DialogParameter;
import com.saop.core.api.DialogParameterCallback;

/**
 * 弹窗相关工具类
 */
public final class DialogUtils {

    private static DialogUtils sInstance;

    private DialogListener mDialogCallback;

    private final DialogParameter mParameters = new DialogParameter();

    private volatile DialogHandler.Statue dialogSelectStatue = DialogHandler.Statue.CANCEL;

    public static DialogUtils dialog() {
        return new DialogUtils();
    }

    private DialogUtils() {
        sInstance = this;
    }

    public DialogUtils callback(final DialogListener callback) {
        mDialogCallback = callback;
        return this;
    }

    public DialogUtils setParameter(final DialogParameterCallback callback) {
        callback.reset(mParameters);
        return this;
    }

    public void show() {
        if (mDialogCallback.isShow(mParameters, null)) {
            DialogActivity.start(SAOP.getContext());
        }
    }

    private void requestCallback() {
        if (mDialogCallback != null) {
            switch (dialogSelectStatue) {
                case POSITIVE: {
                    mDialogCallback.onPositive(mParameters.getPositive());
                    break;
                }
                case NEGATIVE: {
                    mDialogCallback.onNegative(mParameters.getNegative());
                    break;
                }
                case NEUTRAL: {
                    mDialogCallback.onNeutral(mParameters.getNeutral());
                    break;
                }
                case CANCEL: {
                    mDialogCallback.onCancel();
                    break;
                }
            }
            mDialogCallback = null;
        }
    }

    public static class DialogActivity extends Activity {

        public static void start(final Context context) {
            Intent starter = new Intent(context, DialogActivity.class);
            starter.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(starter);
        }

        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if (sInstance == null) {
                finish();
            }
            DialogHandler dialogHandler = SAOP.getDialogHandler();
            final DialogHandler.Statue[] select = {DialogHandler.Statue.CANCEL};
            final Dialog alertDialog;
            if (dialogHandler != null) {
                alertDialog = dialogHandler.createDialog(this, sInstance.mParameters, select);
            } else {
                // 请求权限 , 弹出dialog
                final AlertDialog.Builder builder = new AlertDialog.Builder(this)
                        .setTitle(sInstance.mParameters.getTitle())
                        .setMessage(sInstance.mParameters.getMessage());
                String positive = sInstance.mParameters.getPositive();
                if (!TextUtils.isEmpty(positive)) {
                    builder.setPositiveButton(positive, (dialog, which) -> {
                        select[0] = DialogHandler.Statue.POSITIVE;
                        dialog.dismiss();
                    });
                }

                String negative = sInstance.mParameters.getNegative();
                if (!TextUtils.isEmpty(negative)) {
                    builder.setNegativeButton(negative, (dialog, which) -> {
                        select[0] = DialogHandler.Statue.NEGATIVE;
                        dialog.dismiss();
                    });
                }

                String neutral = sInstance.mParameters.getNeutral();
                if (!TextUtils.isEmpty(neutral)) {
                    builder.setNeutralButton(neutral, (dialog, which) -> {
                        select[0] = DialogHandler.Statue.NEUTRAL;
                        dialog.dismiss();
                    });
                }
                alertDialog = builder.create();
                alertDialog.setCanceledOnTouchOutside(sInstance.mParameters.isTouchOutSideCancel());
            }
            alertDialog.setOnCancelListener(dialog -> {
                select[0] = DialogHandler.Statue.CANCEL;
            });
            alertDialog.setOnDismissListener(dialog -> {
                sInstance.dialogSelectStatue = select[0];
                finish();
            });
            alertDialog.show();
        }

        @Override
        public void finish() {
            super.finish();
            SAOP.handler().post(() -> {
                if (sInstance != null) {
                    sInstance.requestCallback();
                }
            });
        }
    }
}